$(document).ready(function() {
	init();
	
});

//参数
var query = {
		page: 1,
		rows: 15,
		uname: ""
};

function init(){
	query.page = 1;
	queryBalance(1);
}

function queryBalance(pageNum) {
	if (undefined != pageNum) {
		query.page = pageNum;
	} else {
		query.page = 1;
	}
	query.uname = $("#uname").val();
	createQuery();
}

function balanceList(id){
	var editPath = ctx+'/views/toUsedLog';
	if(undefined != id){
		editPath += '?id='+id;
	}
	window.location.href = editPath;
		
}
var editIndex;
function recharge(id){
	var editPath = ctx+'/toCalledEdit';
	if(undefined != id){
		editPath += '?userids='+id;
	}
	editIndex = layer.open({
        type: 2,
        title: '充值',
        shadeClose: true,
        area: ['400px'],
        content: editPath,
        scrollbar: false
    });
}

function rechargeAll(){
	var editPath = ctx+'/toCalledEditAll';
	editIndex = layer.open({
        type: 2,
        title: '充值全部',
        shadeClose: true,
        area: ['400px'],
        content: editPath,
        scrollbar: false
    });
}

function upSec(p_sec){
	var tmpdata = {
			sec:p_sec
	};
	var html = template('myTemplate',tmpdata);
	layer.open({
	    type: 1,
	    skin: 'layui-layer-demo', //样式类名
	    closeBtn: 1, //不显示关闭按钮
	    shift: 2,
	    shadeClose: false, //开启遮罩关闭
	    content: html
	});
//	layer.open({
//        type: 2,
//        title: '修改',
//        shadeClose: true,
//        area: ['400px'],
//        content: html,
//        scrollbar: false
//    });
}

function createQuery(){
//	var uname=$("#uname").val();
	var path = ctx+"/called/queryCalleds";
//	query.uname = uname;
	commonAjax(path,query,function(data){
		if(data.success){
			var pageStr = new Array; 
			$.each(data.rows,function(index,row){
				pageStr.push('<tr>');
				pageStr.push('<td>'+(index+1)+'</td>');
				pageStr.push('<td><a href="#" >'+row.uname+'</a></td>');
				pageStr.push('<td><a href="#" >'+row.nikename+'</a></td>');
				pageStr.push('<td><a href="#" >'+row.sex+'</a></td>');
				pageStr.push('<td><a href="#" >'+row.tel+'</a></td>');
				pageStr.push('<td><a href="#" >'+convertNullToStr(row.money_balance)+'</a></td>');
				pageStr.push('<td><a href="#" >'+convertNullToStr(row.time_balance)+'</a></td>');
				pageStr.push('<td><a href="#" >'+convertNullToStr(row.updated_time)+'</a></td>');
				pageStr.push(' <td>');
				pageStr.push('<div class="am-btn-toolbar">');
				pageStr.push(' <div class="am-btn-group am-btn-group-xs">');
				
				pageStr.push('  <button onclick="recharge('+row.id+')" class="am-btn am-btn-default am-btn-xs am-text-firstly"><span class="am-icon-pencil-square-o"></span> 充值</button>');
				pageStr.push('  <button onclick="balanceList('+row.id+')" class="am-btn am-btn-default am-btn-xs am-text-danger "><span class="am-icon-pencil-square-o"></span> 使用记录</button>');
				
				pageStr.push(' </div>');
				pageStr.push(' </div>');
				pageStr.push(' </td>');
				pageStr.push(' </tr>');
			});
			$("#pagelist").html(pageStr.join(""));
			
			var page_options = {
					id:"content_page",
					pageCount:data.pageCount,
					current:data.pageNumber,
					callback:"queryBalance"
			}
			pages(page_options);
		}else{
			showMsg(data.msg);
		}
	});
}


function showMsg(msg){
	$("#alert-msg").html(msg);
	$("#my-msg").modal();
}

function delUser(id){
	var delPath = ctx+'/user/del';
	var depParam = {
			id:id
	}
	showConfirm("确认删除该条记录吗？",function(){
		commonAjax(delPath,depParam,function(data){
			if(data.success){
				createQuery();
			}
			showMsg(data.msg);
		});
	});
}

